package com.example.administrator.worldcup405;

import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

public class TeamsActivity extends AppCompatActivity {

    ListView mLvTeams;
    ArrayList<Team> teams;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_teams);
        initData();
        initViexw();
    }

    private void initData() {

        String jsonStr = getJsonStr();
        teams = getTeams(jsonStr);
    }

    private ArrayList<Team> getTeams(String jsonStr) {
        ArrayList<Team> teamArrayList = new ArrayList<>();
        try {
            JSONArray jsonArray = new JSONArray(jsonStr);
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                Team team = new Team();
                team.setId(jsonObject.getString("id"));
                team.setGroup(jsonObject.getString("group"));
                team.setName(jsonObject.getString("name"));
                team.setSheng(jsonObject.getInt("sheng"));
                team.setPing(jsonObject.getInt("ping"));
                team.setWingoal(jsonObject.getInt("wingoal"));
                team.setLosegoal(jsonObject.getInt("losegoal"));
                team.setTotalgoal(jsonObject.getInt("totalgoal"));
                team.setScore(jsonObject.getInt("score"));
                team.setPic(jsonObject.getString("pic"));
                team.setDetail(jsonObject.getString("detail"));

                teamArrayList.add(team);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return teamArrayList;
    }

    private String getJsonStr() {
        InputStream inputStream = getResources().openRawResource(R.raw.teams);
        byte[] bytes = new byte[102400];
        try {
            int len = inputStream.read(bytes);
            String str = new String(bytes, 0, len);
            return str;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    private void initViexw() {
        mLvTeams = findViewById(R.id.lv_teams);
        MyAdapter adapter = new MyAdapter();
        mLvTeams.setAdapter(adapter);
    }

    class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return teams.size();
        }

        @Override
        public Object getItem(int position) {
            return teams.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = View.inflate(TeamsActivity.this, R.layout.list_item_layout, null);
            TextView tvGroupId = view.findViewById(R.id.tv_group_id);
            tvGroupId.setText(teams.get(position).getId());
            ImageView ivPic = findViewById(R.id.iv_team);
            return view;
        }
    }
}

